"""
分页控件样式管理模块
"""
from PyQt6.QtWidgets import (QHBoxLayout, QPushButton, QLabel, QSpinBox, QWidget)
from PyQt6.QtCore import Qt

class PaginationWidget(QWidget):
    """分页控件"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.page = 1
        self.page_size = 30
        self.total_count = 0
        self.setup_ui()
    
    def setup_ui(self):
        """初始化UI"""
        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addStretch()
        
        # 按钮样式
        button_style = """
            QPushButton {
                background-color: #1890ff;
                border: none;
                border-radius: 4px;
                color: white;
                padding: 5px 15px;
                font-size: 14px;
            }
            QPushButton:hover {
                background-color: #40a9ff;
            }
            QPushButton:pressed {
                background-color: #096dd9;
            }
            QPushButton:disabled {
                background-color: #d9d9d9;
            }
        """
        
        # 上一页按钮
        self.prev_btn = QPushButton("上一页")
        self.prev_btn.setStyleSheet(button_style)
        
        # 页码标签
        self.page_label = QLabel("第1页/共1页")
        self.page_label.setStyleSheet("padding: 0 10px;")
        
        # 下一页按钮
        self.next_btn = QPushButton("下一页")
        self.next_btn.setStyleSheet(button_style)
        
        # 每页显示数量
        self.page_size_spin = QSpinBox()
        self.page_size_spin.setRange(10, 100)
        self.page_size_spin.setValue(self.page_size)
        self.page_size_spin.setSingleStep(10)
        self.page_size_spin.setStyleSheet("""
            QSpinBox {
                border: 1px solid #d9d9d9;
                border-radius: 4px;
                padding: 4px;
                min-width: 80px;
            }
            QSpinBox:focus {
                border-color: #40a9ff;
            }
        """)
        
        # 总数标签
        self.total_label = QLabel("共0条")
        self.total_label.setStyleSheet("padding: 0 10px;")
        
        # 添加到布局
        layout.addWidget(self.prev_btn)
        layout.addWidget(self.page_label)
        layout.addWidget(self.next_btn)
        layout.addWidget(QLabel("每页显示:"))
        layout.addWidget(self.page_size_spin)
        layout.addWidget(self.total_label)
    
    def update_state(self, current_page: int, total_count: int, page_size: int = None):
        """
        更新分页状态
        :param current_page: 当前页码
        :param total_count: 总记录数
        :param page_size: 每页显示数量
        """
        self.page = current_page
        self.total_count = total_count
        if page_size is not None:
            self.page_size = page_size
            self.page_size_spin.setValue(page_size)
        
        # 计算总页数
        total_pages = (total_count + self.page_size - 1) // self.page_size
        total_pages = max(1, total_pages)
        
        # 更新显示
        self.page_label.setText(f"第{current_page}页/共{total_pages}页")
        self.total_label.setText(f"共{total_count}条")
        
        # 更新按钮状态
        self.prev_btn.setEnabled(current_page > 1)
        self.next_btn.setEnabled(current_page < total_pages)
